﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace B10_Ex05_Elad_Meyron.Models
{
    public class World : GameService
    {
        // the data about the camera location/rotation/features:
        public int k_WorldSize = 100;
        public Vector3 m_PointOfViewPosition;
        private Matrix m_ProjectionFieldOfView;

        public Matrix ProjectionFieldOfView
        {
            get { return m_ProjectionFieldOfView; }
            set { m_ProjectionFieldOfView = value; }
        }

        private Matrix m_PointOfView;

        public Matrix PointOfView
        {
            get { return m_PointOfView; }
            set { m_PointOfView = value; }
        }

        public World(Game i_Game) : base(i_Game)
        {
            float k_NearPlaneDistance = 0.5f;
            float k_FarPlaneDistance = 1000.0f;
            float k_ViewAngle = MathHelper.PiOver4;

            // we are storing the field-of-view data in a matrix:
            m_ProjectionFieldOfView = Matrix.CreatePerspectiveFieldOfView(
                k_ViewAngle,
                Game.GraphicsDevice.Viewport.AspectRatio,
                k_NearPlaneDistance,
                k_FarPlaneDistance);

            // we want to shoot the center of the world:
            Vector3 targetPosition = Vector3.Zero;

            // we are standing 50 units in front of our target:
            m_PointOfViewPosition = new Vector3(0, 0, k_WorldSize);

            // we are not standing on our head:
            Vector3 pointOfViewUpDirection = new Vector3(0, 1, 0);

            // we are storing the point-of-view data in a matrix:
            m_PointOfView = Matrix.CreateLookAt(
                m_PointOfViewPosition, targetPosition, pointOfViewUpDirection);
        }

        public override void Initialize()
        {
            base.Initialize();
            // we are working with colored vertices
            this.Game.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
                this.Game.GraphicsDevice, VertexPositionColor.VertexElements);
            this.Game.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
            this.Game.GraphicsDevice.RenderState.DepthBufferEnable = true;
            this.Game.GraphicsDevice.RenderState.AlphaTestEnable = false;

            this.Game.GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
            this.Game.GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
        }
    }
}
